package com.zmzncs.lmtc.module.customer.controller;


import com.zmzncs.lmtc.common.annotation.Idempotent;
import com.zmzncs.lmtc.common.util.BolianUtil;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.pojo.bolian.PaymonthcardRes;
import com.zmzncs.lmtc.common.util.DateTimeUtil;
import com.zmzncs.lmtc.common.util.ExceptionUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.common.util.WeChatUtil;
import com.zmzncs.lmtc.common.websocket.WebsocketUtil;
import com.zmzncs.lmtc.module.customer.pojo.card.*;
import com.zmzncs.lmtc.module.customer.service.CardService;
import com.zmzncs.lmtc.module.customer.service.CustomerService;
import com.zmzncs.lmtc.module.customer.service.CustTicketService;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户-会员卡 前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-09-02
 */
@Slf4j
@Api(tags = "客户会员卡")
@RestController
@RequestMapping("/customer/card")
public class CardController {

    @Autowired
    CardService cardService;
    @Autowired
    CustomerService customerService;
    @Autowired
    CustTicketService custTicketService;
    @Autowired
    WebsocketUtil websocketUtil;


    @ApiOperation(value = "充值")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "/recharge")
    @Idempotent
    public Rest recharge(@RequestBody @Validated RechargeForm form) {
        Map<String, Object> recharge = cardService.recharge(form);

        return RestUtil.success(recharge);
    }

    @ApiOperation(value = "购买月卡")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "/buyMonthCard")
    @Idempotent
    public Rest buyMonthCard(@Validated MonthCardBuyForm form) {
        Map<String, Object> map = cardService.buyMonthCard(form);

        return RestUtil.success(map);
    }

    @ApiOperation(value = "购买定制卡")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "/buyCustomizeCard")
    @Idempotent
    public Rest buyCustomizeCard(@Valid CustomizeCardBuyForm customizeCardBuyForm) {
        Map<String, Object> map = cardService.buyCustomizeCard(customizeCardBuyForm);

        return RestUtil.success(map);
    }

    @ApiOperation(value = "购买时长卡")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "/buyDurationCard", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @Idempotent
    public Rest buyDurationCard(@Valid DurationCardBuyForm durationCardBuyForm) {
        Map<String, Object> map = cardService.buyDurationCard(durationCardBuyForm);

        return RestUtil.success(map);
    }

    @ApiOperation(value = "微信支付回调-充值")
    @PostMapping(value = "/wechat/recharge/callback")
    public String rechargeCallback(HttpServletRequest request) {
        String callbackStr = WeChatUtil.parseCallback(request);
        if (StringUtils.isBlank(callbackStr)) {
            return WeChatUtil.wxpayCallbackResult(false);
        }

        return cardService.rechargeCallback(callbackStr);
    }

    @ApiOperation(value = "微信支付回调-购买普通月卡")
    @PostMapping(value = "/wechat/buyMonthCard/callback")
    public String buyMonthCardCallback(HttpServletRequest request) {
        String callbackStr = WeChatUtil.parseCallback(request);
        log.info("微信支付回调-购买普通月卡：{}", callbackStr);
        if (StringUtils.isBlank(callbackStr)) {
            return WeChatUtil.wxpayCallbackResult(false);
        }

        return cardService.buyMonthCardCallback(callbackStr);
    }

    @ApiOperation(value = "微信支付回调-购买定制卡")
    @PostMapping(value = "/wechat/buyCustomizeCard/callback")
    public String buyCustomizeCardCallback(HttpServletRequest request) {
        String callbackStr = WeChatUtil.parseCallback(request);
        if (StringUtils.isBlank(callbackStr)) {
            return WeChatUtil.wxpayCallbackResult(false);
        }

        return cardService.buyCustomizeCardCallback(callbackStr);
    }

    @ApiOperation(value = "微信支付回调-购买时长卡")
    @PostMapping(value = "/wechat/buyDurationCard/callback")
    public String buyDurationCardCallback(HttpServletRequest request) {
        String callbackStr = WeChatUtil.parseCallback(request);
        if (StringUtils.isBlank(callbackStr)) {
            return WeChatUtil.wxpayCallbackResult(false);
        }

        return cardService.buyDurationCardCallback(callbackStr);
    }

    @ApiOperation(value = "会员购买之后，判断订单是否支付状态成功")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "header", name = "orderCode", value = "订单号", required = true),
    })
    @PostMapping(value = "validatePayState")
    public Rest<Boolean> validatePayState(@NotNull String orderCode) {
        boolean payResult =  cardService.validatePayState(orderCode);

        return RestUtil.success(payResult);
    }

    @ApiOperation(value = "获取客户服务剩余时间")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @GetMapping(value = "/getCustomerLeftTime")
    public Rest<CardTimeVO> getCustomerLeftTime(){
        List<CardDTO> monthCardDTOList = customerService.getCustomerMonthCardList();
//        List<CardDTO> durationCardDTOList = customerService.getCustomerDurationCard();
        CardDTO rechargeCard = customerService.getRechargeCard();

        //  月卡剩余时间
        long dayLeft = 0;
        for(CardDTO cardDTO: monthCardDTOList){
            dayLeft = dayLeft + DateTimeUtil.getDistanceDays(new Date(), cardDTO.getExpireTime());
        }

        CardTimeVO cardTimeVO = new CardTimeVO();
        cardTimeVO.setDayLeft(dayLeft);
        cardTimeVO.setTimeLeft(rechargeCard == null || rechargeCard.getCardBalance() == null ? "0" : rechargeCard.getCardBalance().toString());

        return RestUtil.success(cardTimeVO);
    }

    @ApiOperation(value = "查询用户充值信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "/getUserRechargeInfo")
    public Rest<RechargeInfoVO> getUserRechargeInfo() {
        RechargeInfoVO rechargeInfo = cardService.getUserRechargeInfo();
        return RestUtil.success(rechargeInfo);
    }

}

